package com.hfxt.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfxt.entity.User;
import com.hfxt.mapper.UserMapper;
import com.hfxt.util.RedisUtil;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

//@Repository
@Service("userService")
//事务处理
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisUtil<User> redisUtil;


    //登录
    @Override
//    @Transactional
    public User userLogin(String userName) {
        return userMapper.userLogin(userName);
    }

    //条件分页 查询用户列表
    @Override
//    @Transactional
    public List<User> queryAllUsersByCondition(String userName, Integer userRole, Integer pageIndex, Integer pageSize) {
//        List<User> userList = null;
//        System.out.println("userService---" + userName + " " + userRole + " " + pageIndex + " " + pageSize);
//        System.out.println("redisUtil.hasKey(\"userList\") = " + redisUtil.hasKey("userList"));
//        if (redisUtil.hasKey("userList")) {
//            redisUtil.del("userList");
//            System.out.println("redisUtil.hasKey2222222222(\"userList\") = " + redisUtil.hasKey("userList"));
//        }
//        redisUtil.lSet("userList", userMapper.queryAllUsersByCondition(userName, userRole, (pageIndex - 1) * pageSize, pageSize));
//        userList = redisUtil.lGet("userList", 0, -1);
//        for (User user : userList) {
//            System.out.println("redis中的'userList'中的user = " + user);
//
//        }
//
//        return userList;
        return userMapper.queryAllUsersByCondition(userName, userRole, (pageIndex - 1) * pageSize, pageSize);
    }

    //条件分页 查询总记录数
    @Override
//    @Transactional
    public int queryCountByCondition(String userName, Integer userRole) {
        return userMapper.queryCountByCondition(userName, userRole);
    }

    //查询用户详细信息
    @Override
//    @Transactional
    public User queryUserById(Long id) {
        return userMapper.queryUserById(id);
    }

    //删除用户
    @Override
//    @Transactional
    public int deleteUserById(Long id) {
        //事务处理异常测试
//        String s = null;
//        System.out.println(s.toCharArray());
        //删除redis缓存中的旧userListAll数据
        if (redisUtil.hasKey("userListAll")) {
            redisUtil.del("userListAll");
        }
        return userMapper.deleteUserById(id);
    }

    //修改用户
    @Override
//    @Transactional
    public int updateUser(User user) {
        //删除redis缓存中的旧userListAll数据
        if (redisUtil.hasKey("userListAll")) {
            redisUtil.del("userListAll");
        }
        return userMapper.updateUser(user);
    }

    //新增用户
    @Override
//    @Transactional
    public int addUser(User user) {
        //删除redis缓存中的旧userListAll数据
        if (redisUtil.hasKey("userListAll")) {
            redisUtil.del("userListAll");
        }
        return userMapper.addUser(user);
    }

    //查询所有用户
    @Override
//    @Transactional
//    @Cacheable(value = "redisCacheManager",key = "'queryAllUsers'")
    public List<User> queryAllUsers() {
        List<User> userListAll = null;
        if (!redisUtil.hasKey("userListAll")) {//redis中没有
            userListAll = userMapper.queryAllUsers();
            System.out.println("redis中没有该内容，正在创建！");
            redisUtil.lSet("userListAll", userListAll);
        } else {
            System.out.println("直接读取redis中的内容！");
            userListAll = redisUtil.lGet("userListAll", 0, -1);
        }
//        System.out.println("没有走redis缓存就打印我。。。。。。。。。");
        return userListAll;
    }

    @Override
    public List<User> queryAllUsersByPageHelper(String userName, Integer userRole) {
        return userMapper.queryAllUsersByPageHelper(userName, userRole);
    }

    @Override
    public PageInfo<User> queryAllUsersByPageInfo(String userName, Integer userRole, Integer pageNum, Integer pageSize) {
        System.out.println("pageNum = " + pageNum);
        System.out.println("pageSize = " + pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.queryAllUsersByPageHelper(userName, userRole);
        return new PageInfo<User>(userList);
    }

    @Override
    public int deleteUsersByList(List<Integer> list) {
        //删除redis缓存中的旧userListAll数据
        if (redisUtil.hasKey("userListAll")) {
            redisUtil.del("userListAll");
        }
        return userMapper.deleteUsersByList(list);
    }
}
